package com.by.servlet;

import com.by.dao.tbDao;
import com.by.util.JsonUtil;
import com.by.util.PageUtil;
import com.by.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.by.entity.tb;

@WebServlet("/tbPage")
public class tbQueryPageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        int pageNum = Integer.parseInt(request.getParameter("pageNum"));
        String user_name = request.getParameter("user_name");

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("pageNum",pageSize * (pageNum - 1));
        params.put("pageSize",pageSize);
        params.put("user_name",user_name);

        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        tbDao tbdao = session.getMapper(tbDao.class);
        List<tb> list = tbdao.gettbsByPage(params);
        int count = tbdao.gettbCount();
        //总页数
        int totalPages = PageUtil.getTotalPages(count,pageSize);

        Map<String,Object> maps = new HashMap<String,Object>();
        maps.put("tbList",list);
        maps.put("pageNum",pageNum);
        maps.put("pageSize",pageSize);
        maps.put("totalPages",totalPages);
        JsonUtil.getJson(request,response,maps);

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
